package com.gzsz.service;

import com.gzsz.util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;

import static java.lang.System.*;

public class WardServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        DBUtil dbUtil=new DBUtil();
        out.println("entry -----");
        String method= request.getParameter("method");
        if (method.equals("delete")){
            String ward_id= request.getParameter("ward_id");
            String delete_sql="delete from ward where ward_id="+ward_id+";";
            if (dbUtil.executeUpdateBySQL(delete_sql)>0){
                resp.sendRedirect("/webapps_Web_exploded/ward?doctor_id=&ward_type=&method=search");
            }else {
                out.println("删除失败");
            }
        }
        else if (method.equals("search")){
            String ward_type = Optional.ofNullable(request.getParameter("ward_type")).orElse(""); // 直接赋值，无需初始化
            String doctor_id = Optional.ofNullable(request.getParameter("doctor_id")).orElse("");
            String query_sql;
            out.println(ward_type.isEmpty() && doctor_id.isEmpty());
            out.println(doctor_id);
            // 根据参数构建SQL查询
            if ((ward_type.isEmpty()) && (doctor_id.isEmpty())) {
                query_sql = "select * from ward";
            }
            else if (!ward_type.isEmpty() && !doctor_id.isEmpty()) {
                query_sql = "select * from ward where doctor_id like '%" + doctor_id + "%' and ward_type like '%" + ward_type + "%'";
            }
            else if (!doctor_id.isEmpty()) {
                query_sql = "select * from ward where doctor_id like '%" + doctor_id + "%'";
            }
            else {
                query_sql = "select * from ward where ward_type like '%" + ward_type + "%'";
            }
            out.println(query_sql);
            // 执行查询
            ResultSet resultSet = dbUtil.queryBySQL(query_sql);
            request.setAttribute("ward_list", resultSet);
            request.getRequestDispatcher("/html1/ward.jsp").forward(request, resp);
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException{
        String method= request.getParameter("method");
        DBUtil dbUtil=new DBUtil();
        if (method.equals("add")){
            //1.获取表单的值
            String ward_type = request.getParameter("ward_type");
            String bed_number = request.getParameter("bed_number");
            String doctor_id = request.getParameter("doctor_id");
            //2.拼接SQL语句执行数据库
            String add_sql="INSERT INTO ward (ward_type, bed_number, doctor_id) " +
                    "VALUES ('"+ward_type+"', '"+bed_number+"', '"+doctor_id+"');";
            if (dbUtil.executeUpdateBySQL(add_sql)>0){
                resp.sendRedirect("/webapps_Web_exploded/ward?doctor_id=&ward_type=&method=search");
            }else {
                out.println("添加失败");
            }
        } else if (method.equals("edit")) {

            //1.获取表单的值
            String ward_id = request.getParameter("ward_id");
            String ward_type = request.getParameter("ward_type");
            String bed_number = request.getParameter("bed_number");
            String doctor_id = request.getParameter("doctor_id");
            //2.拼接SQL语句执行数据库
            String edit_sql="UPDATE ward SET ward_id='"+ward_id+"',ward_type='"+ward_type+"',bed_number='"+bed_number+"',doctor_id='"+doctor_id+"'where ward_id='"+ward_id+"'";

            if (dbUtil.executeUpdateBySQL(edit_sql)>0){
//                System.out.println("编辑成功");
                resp.sendRedirect("/webapps_Web_exploded/ward?doctor_id=&ward_type=&method=search");
            }else {
                out.println("编辑失败");
            }
        }
    }
}




